home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
MACSHELL
/
MS1
/
COMMANDS
/
USAGEMAN.C
< prev
next >
Wrap
Text File
|
1992-12-02
|
11KB
|
471 lines
/*
* MacShell Source File
*
* Copyright (c) 1989, 1990, 1991, 1992 Suick Bay Technologies. All rights reserved.
*
*
* RESTRICTIONS ON MacShell program and source code.
*
* Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
* restricted use by the owner of the CDROM "Disk to the future II".
*
* Ñ╩No permission is granted for any commercial use without the written
* consent of the Suick Bay Technologies.
*
* Ñ╩No permission is granted for any redistribution of any kind use without
* the written consent of the Suick Bay Technologies.
*
* Ñ╩Permission is granted to use this for any personal noncommercial use.
*
* Ñ╩You may not distribute source or executable code at all, nor may you
* distribute it with or within a commercial product without the written
* consent of the Suick Bay Technologies. Please send modifications to
* the author for inclusion in updates to the program. Thanks.
*
*
* MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
* WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
*
* SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
* INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
* OR ANY PART THEREOF.
*
* In no event will Suick Bay Technologies be liable for any lost revenue
* or profits or other special, indirect and consequential damages, even if
* Suick Bay Technologies has been advised of the possibility of such damages.
*
* Suick Bay Technologies can be reached at:
*
* 8768 Cottonwood lane
* Maple Grove, MN 55369
* Voice: (612) 425-7025
* AppleLink: D5233
*
*
* No parts of this software may be reproduced or stored in a
* retrieval system or transmitted in any form, or any means,
* electronic, mechanical, photocopying, recording or otherwise,
* without the prior written permission of Suick Bay Technologies.
*
* Spread the word and not the disk.
*
* SPK 012490 : Added no man avail. message.
* SPK 012290 : Initial
*/
#include <ResourceMgr.h>
#include "SystemPub.h"
#include "Proc.h"
#include "ShellPub.h"
#include "Path.h"
extern int16 appResFile;
/*******************************************************************/
#define MUechomode (**MyShell).Proc[ProcID].bflags.f0
DumpResText( WHandle ShellWh, int16 ProcID, char *name, int32 type )
{
char buf[ 256 ], *cp, **ch;
int16 i;
int32 length;
strcpy( buf, name );
CtoPstr( buf );
UseResFile( appResFile );
ch = (char **) GetNamedResource( type, buf );
LoadResource( ch );
if( ch )
{
DoResTextDump: /* a code saving goto */
HLock( ch );
cp = *ch;
length = GetHandleSize( ch );
while( length )
{
i = 0;
while( i < 255 )
{
buf[ i ] = *cp;
if( (*cp == '\n') || (*cp == '\r') )
{
buf[ ++i ] = '\0';
cp++;
length--;
StdOut( ShellWh, ProcID, buf );
break;
}
i++;
cp++;
length--;
}
if( i == 255 ) /* filled the buffer */
{
buf[ ++i ] = '\0';
StdOut( ShellWh, ProcID, buf );
}
}
HUnlock( ch );
}
else /* was it an alias ? */
{
ch = GetShellString( name );
if( ch )
{
CopyStr( *ch, buf );
buf[0]--;
ch = (char **) GetNamedResource( type, buf );
if( ch )
{
StdOut( ShellWh, ProcID, "ALIAS COMMAND\n" );
goto DoResTextDump;
}
else
goto NoResText; /* save a little code */
}
else
{
NoResText:
procPrintf( ShellWh, ProcID, "man : no manual for %s\n",
name );
}
}
}
/*******************************************************************/
Boolean DoUSAGE( int16 ProcToken, WHandle ShellWh, int16 ProcID,
char *string )
{
int16 i, argc;
char *cp, argument[ 256 ];
ShellWindRec **MyShell = (ShellWindRec **) (**ShellWh).thing;
switch( ProcToken )
{
case PROC_INIT :
(**MyShell).Proc[ ProcID ].flags = TRUE;
break;
case PROC_TERM :
case PROC_BREAK :
/* Tell the shell that we're done */
SendOutToken( ShellWh, ProcID, PROC_BREAK );
/* Turn ourself off */
(**MyShell).Proc[ ProcID ].ProcActive = FALSE;
break;
case PROC_STDIN :
if( (**MyShell).Proc[ ProcID ].flags )
{
(**MyShell).Proc[ ProcID ].flags = FALSE;
if( argc == 1 )
DumpResText( ShellWh, ProcID, "usage", MANRES );
else
for( i = 1; i < (**MyShell).Proc[ ProcID ].argc; i++ )
{
GetArgv( ShellWh, ProcID, i, argument );
DumpResText( ShellWh, ProcID, argument, USERES );
}
/* Tell the shell that we're done */
SendOutToken( ShellWh, ProcID, PROC_BREAK );
/* Turn ourself off */
(**MyShell).Proc[ ProcID ].ProcActive = FALSE;
return( FALSE );
}
}
}
/*******************************************************************/
WipeMANS()
{
int16 i;
Handle h;
UseResFile( appResFile );
i = Count1Resources( MANRES );
while( i )
{
h = GetIndResource( MANRES, i );
RmveResource( h );
DisposHandle( h );
i--;
}
i = Count1Resources( USERES );
while( i )
{
h = GetIndResource( USERES, i );
RmveResource( h );
DisposHandle( h );
i--;
}
}
/*******************************************************************/
#define BUFSIZE 32
void MANCCallBack( WHandle ShellWh, int16 ProcID, char *path,
char *last, pathType what, int16 vRefNum, int32 dirID )
{
char *cp, str[ BUFSIZE ], name[ 32 ];
int16 fRefNum, sErr = noErr, id;
FInfo finder;
int32 count = 1L, resSize, start, end, type = 0L;
Handle h;
ShellWindRec **MyShell = (ShellWindRec **) (**ShellWh).thing;
if( what == pathIsFile )
{
strcpy( str, last );
CtoPstr( str );
sErr = HGetFInfo( vRefNum, dirID, str, &finder );
if( !sErr && (finder.fdType == 'TEXT' ))
sErr = HOpen( vRefNum, dirID, str, fsRdPerm, &fRefNum );
while( !sErr && !UserAbort())
{
count = 1L;
cp = str;
sErr = FSRead( fRefNum, &count, cp );
CursorWait();
if( !sErr )
{
if( *cp == '#' ) /* comment, read until next line */
{
while( !sErr )
{
sErr = FSRead( fRefNum, &count, cp );
if( sErr || (*cp == '\n') || (*cp == '\r') )
break;
}
}
else if( *cp == '<' )
{
sErr = FSRead( fRefNum, &count, cp );
if( !sErr )
{
if( *cp == 'U' ) /* usage specification */
type = USERES;
else if( *cp == 'M' ) /* man specification */
type = MANRES;
if( type )
{
while( !sErr ) /* read past '> ' to name */
{
sErr = FSRead( fRefNum, &count, cp );
if( sErr || ((*cp != ' ') && (*cp != '>')) )
break;
}
*name = *cp;
cp = name;
cp++;
while( !sErr ) /* read name */
{
sErr = FSRead( fRefNum, &count, cp );
if( sErr || (*cp == '\n') || (*cp == '\r') )
break;
cp++;
}
*cp = '\0';
CtoPstr( name ); /* use name */
cp = str;
sErr = GetFPos( fRefNum, &start );
sErr = FSRead( fRefNum, &count, cp );
while( !sErr ) /* go till next command */
{
if( !sErr && ((*cp == '\n') || (*cp == '\r')) )
{
sErr = FSRead( fRefNum, &count, cp );
if( sErr || (*cp == '#') || (*cp == '<') )
break;
}
else
sErr = FSRead( fRefNum, &count, cp );
}
if( !sErr )
{
GetFPos( fRefNum, &end );
resSize = end - start - 1;
sErr = SetFPos( fRefNum, fsFromStart, start );
if( !sErr )
{
h = NewHandle( resSize );
if( h )
{
HLock( h );
cp = *h;
sErr = FSRead( fRefNum, &resSize, cp );
HUnlock( h );
if( !sErr )
{
UseResFile( appResFile );
id = Unique1ID( type );
SetResAttrs( h, resPurgeable );
AddResource( h, type, id, name );
}
else
DisposHandle( h );
if( MUechomode )
procPrintf( ShellWh, ProcID,
"%6s %16ps ID %5d\n",
type == USERES ? "Usage" : "Man",
name, id );
}
}
}
}/* eo if type */
}
} /* eo if '<' */
}
}
if( fRefNum ) /* if a file open, close it */
FSClose( fRefNum );
}
}
/*******************************************************************/
CompileMANS( WHandle ShellWh, int16 ProcID, char *argument )
{
ShellWindRec **MyShell;
MyShell = (ShellWindRec **) (**ShellWh).thing;
ExpandPath( ShellWh, ProcID, argument, (ProcPtr) MANCCallBack,
(**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
ResetShellPWD( ShellWh );
}
/*******************************************************************/
Boolean DoMAN( int16 ProcToken, WHandle ShellWh, int16 ProcID,
char *string )
{
int16 i, argc, wmode = FALSE, cmode = FALSE;
char *cp, argument[ 256 ];
ShellWindRec **MyShell = (ShellWindRec **) (**ShellWh).thing;
switch( ProcToken )
{
case PROC_INIT :
(**MyShell).Proc[ ProcID ].flags = TRUE;
break;
case PROC_TERM :
case PROC_BREAK :
/* Tell the shell that we're done */
SendOutToken( ShellWh, ProcID, PROC_BREAK );
/* Turn ourself off */
(**MyShell).Proc[ ProcID ].ProcActive = FALSE;
break;
case PROC_STDIN :
if( (**MyShell).Proc[ ProcID ].flags )
{
(**MyShell).Proc[ ProcID ].flags = FALSE;
argc = (**MyShell).Proc[ ProcID ].argc;
MUechomode = FALSE;
if( argc == 1 )
DumpResText( ShellWh, ProcID, "man", MANRES );
else
{
for( i = 1; i < argc; i++ )
{
GetArgv( ShellWh, ProcID, i, argument );
cp = argument;
if( *cp++ == '-' )
{
if( *cp == '\0' ) /* read from stdin */
(**MyShell).Proc[ ProcID ].options = 1L;
else
while( *cp )
switch( *cp++ )
{
case 'c' : /* compile file */
cmode = TRUE;
break;
case 'w' : /* wipe existin mans/usages */
wmode = TRUE;
break;
case 'e' : /* echo lines */
MUechomode = TRUE;
break;
}
}
}
if( wmode || cmode )
{
if( wmode )
WipeMANS();
if( cmode )
for( i = 1; i < argc; i++ )
{
GetArgv( ShellWh, ProcID, i, argument );
if( *argument != '-' )
CompileMANS( ShellWh, ProcID, argument );
}
}
else
for( i = 1; i < argc; i++ )
{
GetArgv( ShellWh, ProcID, i, argument );
DumpResText( ShellWh, ProcID, argument, MANRES );
}
}
/* Tell the shell that we're done */
SendOutToken( ShellWh, ProcID, PROC_BREAK );
/* Turn ourself off */
(**MyShell).Proc[ ProcID ].ProcActive = FALSE;
return( FALSE );
}
}
}